package com.ceair.mub2c.payment.payments.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ceair.flight.common.exception.ServiceException;
import com.ceair.mub2c.common.utils.GsonUtil;
import com.ceair.mub2c.common.utils.PropertiesUtil;
import com.ceair.mub2c.payment.payments.vo.WalletVO;
import com.ceair.mub2c.payment.payments.vo.WalleterDto;
import com.ceair.mub2c.payment.payments.vo.WalleterPwdVO;
import com.ceair.mub2c.payment.payments.service.CeairWalletService;
import com.ceair.mub2c.payment.payments.utils.HttpClientUtil;
import com.ceair.mub2c.payment.payments.utils.RandomVO;
import com.ceair.mub2c.payment.payments.utils.WalletUtils;
import com.ceair.mub2c.payment.payments.vo.AccountVerifyVO;
import com.ceair.mub2c.payment.payments.vo.WalletAcquireReq;
import com.ceair.mub2c.payment.payments.vo.WalletAcquireRes;
@Service("ceairWalletService")
public class CeairWalletServiceImpl implements CeairWalletService {

	private static Logger log = Logger.getLogger(CeairWalletServiceImpl.class);

	/** AES加密密钥 */ // 暂定，具体密钥需统一支付提供
	public String aeskey = PropertiesUtil.getValue("config.properties", "RANDOM_ACQUIRE_ASE_KEY");
	/** md5签名密钥 */ // 暂定，具体密钥需统一支付提供
	public String md5key = PropertiesUtil.getValue("config.properties", "RANDOM_ACQUIRE_MD5_KEY");
	/**  提供随机数接口路径 */
	public String randomAcquireUrl = PropertiesUtil.getValue("config.properties", "RANDOM_ACQUIRE_URL");
	/**  提供随机数接口路径 */
	public String userIdAcquireUrl = PropertiesUtil.getValue("config.properties", "WALLET_ACQUIRE_URL");
	/**  提供随机数接口路径 */
	public String checkPwdUrl = PropertiesUtil.getValue("config.properties", "WALLETCHECK_PWD_URL");

	/**
	 * 调用muservice获取随机数
	 * @param walletRandomAcquireReq
	 * @return
	 */
	public WalletAcquireRes getWalletRandomAcquire(WalletAcquireReq walletRandomAcquireReq) {
		log.info("调用MUservice获取钱包随机数 入参 : " + walletRandomAcquireReq.toString());
		WalletAcquireRes walletRandomAcquireRes = new WalletAcquireRes();
		String scCode = walletRandomAcquireReq.getScCode();
		String subScCode = walletRandomAcquireReq.getSubScCode();

		String reqId = walletRandomAcquireReq.getReqId();
		String userId = walletRandomAcquireReq.getUserId();
		String customerId = walletRandomAcquireReq.getCustomerId();
		String userType = walletRandomAcquireReq.getUserType();
		String orgCode = walletRandomAcquireReq.getOrgCode();
		String randType = walletRandomAcquireReq.getRandType();

		try {
			log.info("一级/二级销售渠道:" + scCode + "/" + subScCode + " 用户类型:" + userType + " 获取钱包加密随机数开始...");
			// 转换请求参数为json格式
			Map<String, String> bodyMap = new HashMap<String, String>();
			JSON bodyJson = new JSONObject();
			bodyMap.put("reqId", reqId);
			bodyMap.put("userId", userId);
			bodyMap.put("customerId", customerId);
			bodyMap.put("userType", userType);
			bodyMap.put("orgCode", orgCode);
			bodyMap.put("randType", randType);
			bodyJson = JSONObject.parseObject(JSON.toJSONString(bodyMap));

			// 请求参数体加密
			String body = WalletUtils.getAesmisencrypt(bodyJson.toJSONString(), aeskey);

			// 获取签名值
			String sign = WalletUtils.getMd5Sign(body, md5key);

			// 获取随机数请求参数
			Map<String, String> respMap = new HashMap<String, String>();

			respMap.put("scCode", scCode);
			respMap.put("subScCode", subScCode);
			respMap.put("body", body);
			respMap.put("sign", sign);
			String resJson = GsonUtil.toJson(respMap);
			String respCode = HttpClientUtil.httpPostRaw(randomAcquireUrl, resJson, null, null);

			WalletVO fromJson = GsonUtil.fromJson(respCode, WalletVO.class);
			String respBody = fromJson.getBody();
			String respSign = fromJson.getSign();

			// 验签
			Boolean isChange = WalletUtils.checkResp(respBody, respSign, "abc");
			log.info("获取钱包随机数验签 :" + isChange);
			if (!isChange) {
				log.info("验签成功");
				// 验签成功
				String jsonBodyStr = WalletUtils.getAESDeciphering(respBody, aeskey);
				RandomVO randomVO = GsonUtil.fromJson(jsonBodyStr, RandomVO.class);
				// 转换json字符串为json对象并取值
				walletRandomAcquireRes.setRespCode(randomVO.getRespCode());
				walletRandomAcquireRes.setRespDesc(randomVO.getRespDesc());
				walletRandomAcquireRes.setRandom(randomVO.getRandom());
				walletRandomAcquireRes.setRandJnlNo(randomVO.getRandJnlNo());
				walletRandomAcquireRes.setResultCode(randomVO.getRespCode());
				walletRandomAcquireRes.setResultMsg(randomVO.getRespDesc());
				log.info("调用muservice获取随机数返回参数 :" + walletRandomAcquireRes.toString());
				return walletRandomAcquireRes;
			} else {
				// 验签失败
				walletRandomAcquireRes.setResultCode("S2003");
				walletRandomAcquireRes.setResultMsg("钱包积分支付:验签失败！数据可能被修改，存在风险！");
				return walletRandomAcquireRes;
			}
		} catch (ServiceException e) {
			log.error("钱包积分支付 :调用服务失败！" + e);
			walletRandomAcquireRes.setResultCode("S2002");
			walletRandomAcquireRes.setResultMsg("钱包积分支付:调用服务失败！");
			return walletRandomAcquireRes;
		} catch (Exception e) {

			log.error("钱包积分支付 :获取随机数失败！" + e);
			walletRandomAcquireRes.setResultCode("S2001");
			walletRandomAcquireRes.setResultMsg("钱包积分支付:获取随机数失败！");
			return walletRandomAcquireRes;
		}

	}

	/**
	 * 调用统一支付查询常客钱包信息
	 * 
	 * @param walletRandomAcquireReq
	 * @return
	 */
	public WalleterDto getUserInfo(WalletAcquireReq walletRandomAcquireReq) {
		log.info("调用统一支付查询常客钱包信息入参 :" + walletRandomAcquireReq.toString());
		String scCode = walletRandomAcquireReq.getScCode();
		String subScCode = walletRandomAcquireReq.getSubScCode();
		String reqId = walletRandomAcquireReq.getReqId();
		String customerId = walletRandomAcquireReq.getCustomerId();
		String userType = walletRandomAcquireReq.getUserType();
		// 转换请求参数为json格式
		Map<String, String> bodyMap = new HashMap<String, String>();
		JSON bodyJson = new JSONObject();
		bodyMap.put("reqId", reqId);
		bodyMap.put("customerId", customerId);
		bodyMap.put("userType", userType);
		bodyJson = JSONObject.parseObject(JSON.toJSONString(bodyMap));
		// 请求参数体加密
		String body = WalletUtils.getAesmisencrypt(bodyJson.toJSONString(), aeskey);
		// 获取签名值
		String sign = WalletUtils.getMd5Sign(body, md5key);
		// 获取随机数请求参数
		Map<String, String> respMap = new HashMap<String, String>();
		respMap.put("scCode", scCode);
		respMap.put("subScCode", subScCode);
		respMap.put("body", body);
		respMap.put("sign", sign);
		String resJson = GsonUtil.toJson(respMap);
		String respCode = HttpClientUtil.httpPostRaw(userIdAcquireUrl, resJson, null, null);
		WalletVO fromJson = GsonUtil.fromJson(respCode, WalletVO.class);
		String respBody = fromJson.getBody();
		String jsonBodyStr = WalletUtils.getAESDeciphering(respBody, aeskey);
		// 获取钱包用户
		WalleterDto walleter = GsonUtil.fromJson(jsonBodyStr, WalleterDto.class);
		log.info("调用统一支付查询常客钱包信息出参 :" + walleter.toString());
		String code = walleter.getRespCode();
		switch (code) {
		case "success":
			break;
		case "input_not_valid":
			log.info("钱包用户查询:输入不合法");
			break;
		case "user_not_found":
			log.info("钱包用户查询:用户不存在");
			break;
		case "sys_error":
			log.info("钱包用户查询:系统异常");
			break;
		default:
			break;
		}
		return walleter;
	}
	/**
	 * 调用统一支付验证钱包密码
	 * 
	 * @param walletRandomAcquireReq
	 * @return
	 */
	public WalleterPwdVO accountVerifyPwd(AccountVerifyVO accountVerify) {
		log.info("调用统一支付验证钱包密码 :" + accountVerify.toString());
		String scCode = accountVerify.getScCode();
		String subScCode = accountVerify.getSubScCode();
		// 转换请求参数为json格式
		Map<String, String> bodyMap = new HashMap<String, String>();
		JSON bodyJson = new JSONObject();
		bodyMap.put("reqId", accountVerify.getReqId());
		bodyMap.put("customerId", accountVerify.getCustomerId());
		bodyMap.put("userType", accountVerify.getUserType());
		bodyMap.put("orgCode", accountVerify.getOrgCode());
		bodyMap.put("pwdResult", accountVerify.getPwdResult());
		bodyMap.put("random", accountVerify.getRandom());
		bodyMap.put("randJnlNo", accountVerify.getRandJnlNo());
		bodyJson = JSONObject.parseObject(JSON.toJSONString(bodyMap));
		// 请求参数体加密
		String body = WalletUtils.getAesmisencrypt(bodyJson.toJSONString(), aeskey);
		// 获取签名值
		String sign = WalletUtils.getMd5Sign(body, md5key);
		// 获取随机数请求参数
		Map<String, String> respMap = new HashMap<String, String>();
		respMap.put("scCode", scCode);
		respMap.put("subScCode", subScCode);
		respMap.put("body", body);
		respMap.put("sign", sign);
		String resJson = GsonUtil.toJson(respMap);
		String respCode = HttpClientUtil.httpPostRaw(checkPwdUrl, resJson, null, null);
		WalletVO fromJson = GsonUtil.fromJson(respCode, WalletVO.class);
		String respBody = fromJson.getBody();
		String jsonBodyStr = WalletUtils.getAESDeciphering(respBody, aeskey);
		// 获取钱包用户
		WalleterPwdVO walleterPwd = GsonUtil.fromJson(jsonBodyStr, WalleterPwdVO.class);
		log.info("调用统一支付查询常客钱包信息出参 :" + walleterPwd.toString());
		return walleterPwd;
	}

	public static void main(String[] args) {
		/*reqId	请求流水号
		userId	钱包用户号
		customerId	常旅客号
		userType	用户类型
		orgCode	机构号
		pwdResult	电子账户交易密码密文
		randJnlNo	密码随机数流水号
		random	密码随机数*/
		CeairWalletServiceImpl ceairWalletUtils = new CeairWalletServiceImpl();
		WalletAcquireReq walletRandomAcquireReq = new WalletAcquireReq("B2CSC1003", "SUBB2CSC1001",
				"1231dasdasd123123ds", "613010714006", "", "mufly", "ceair", "1");
		WalletAcquireRes walletRandomAcquire = ceairWalletUtils.getWalletRandomAcquire(walletRandomAcquireReq);
		System.out.println(walletRandomAcquire.toString());
	}
}
